/**
 * @program: LeetCode
 * @description: LeetCode : 45. 跳跃游戏 II
 * @author: WXY
 * @create: 2023-01-03 15:30
 * @Version 1.0
 **/
public class Num045_jump {

    public static int jump(int[] nums) {
        int index = 0; // 覆盖的最远下标
        int range = 0;
        int len = nums.length - 1;
        int ans = 0;
        for (int i = 0; i < nums.length; i++) {
            index = Math.max(index, i + nums[i]);
            if (i == range) {
                if (range != nums.length - 1) {
                    ans++;
                    range = index;
                    if (index >= len) break;
                } else {
                    break;
                }
            }

        }
        return ans;
    }

    public static int jump1(int[] nums) {
        int len = nums.length;
        if (len == 1) return 0;
        int k = 0;//记录能够到达的最大位置
        int ans = 0;
        int end = 0;
        for (int i = 0; i < len; i++) {
            k = Math.max(k, i + nums[i]);
            if (k >= len - 1) {
                return ans + 1;
            }
            if (end == i) {
                ans++;
                end = k;
            }
        }
        return ans;
    }

    public static int jump2(int[] arr) {
        int longestDistance = 0;
        int end = 0;
        int ans = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            longestDistance = Math.max(longestDistance, i + arr[i]);
            if (i == end) {
                end = longestDistance;
                ans++;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {2, 3, 1, 0, 2, 5, 0, 2, 6, 0, 1, 4};
        int jump = jump2(arr);
        System.out.println(jump);

    }
}
